package com.bytz.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.aspect.annotation.AutoLog;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.system.constant.SysAuthConstants;
import com.bytz.modules.system.entity.SysUserToken;
import com.bytz.modules.system.service.ISysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 用户长期Token表 前端控制器
 */
@Slf4j
@RestController
@Api(tags = "系统用户长期令牌")
@RequestMapping("/sys/userToken")
public class SysUserTokenController {


    @Autowired
    private ISysUserTokenService sysUserTokenService;

    /**
     * 分页列表查询
     */
    @ApiOperation(value="系统用户长期令牌-分页列表查询")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.UserTokenPermission.TOKEN_VIEW + "')")
    public Result<IPage<SysUserToken>> queryPageList(SysUserToken sysConfig,
                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<SysUserToken> queryWrapper = QueryGenerator.initQueryWrapper(sysConfig, req.getParameterMap());
        Page<SysUserToken> page = new Page<>(pageNo, pageSize);
        IPage<SysUserToken> pageList = sysUserTokenService.queryPage(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param userToken 系统参数
     * @return
     */
    @AutoLog(value = "系统参数-添加")
    @ApiOperation(value="系统用户长期令牌-添加")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.UserTokenPermission.TOKEN_EDIT + "')")
    @PostMapping(value = "/save")
    public Result<SysUserToken> save(@RequestBody @Valid SysUserToken userToken) {
        return Result.ok("保存成功！", sysUserTokenService.saveOrUpdateSaveObject(userToken, true));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "系统用户长期令牌-通过id删除")
    @ApiOperation(value="系统用户长期令牌-通过id删除")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.UserTokenPermission.TOKEN_EDIT + "')")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST })
    public Result<Boolean> delete(@RequestParam(name="id") String id) {
        sysUserTokenService.checkAndRemoveById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="系统用户长期令牌-通过id查询")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.UserTokenPermission.TOKEN_VIEW + "')")
    @GetMapping(value = "/queryById")
    public Result<SysUserToken> queryById(@RequestParam(name="id") String id) {
        return Result.ok(sysUserTokenService.getSaveVoById(id));
    }
}
